package pl.engine.ui.gui.panel
{
	import pl.engine.camera.Camera;
	import pl.engine.model.MapGrid;
	import pl.engine.ui.UI;

	import starling.display.Quad;
	import starling.display.Sprite;
	import starling.text.BitmapFont;
	import starling.text.TextField;
	import starling.utils.HAlign;
	import starling.utils.VAlign;

	import flash.display.MovieClip;
	import flash.geom.Point;

	/**
	 * ...
	 * @author Bakudo
	 */
	public class InfoPanel extends Sprite
	{
		private var bg:Quad;
		// ...
		private var labelMapInfo:TextField;
		private var labelBaseTextureSize:TextField;
		private var labelRectTileDimension:TextField;
		private var labelMapTilesCount:TextField;
		// ...
		private var labelViewInfo:TextField;
		private var labelTilePos:TextField;
		private var labelRectPos:TextField;
		private var labelIsoPos:TextField;
		// ...
		private static var _instance:InfoPanel;

		public function InfoPanel( mc:MovieClip )
		{
			bg = new Quad( mc.width, 1, 0 );
			addChild( bg );
			labelMapInfo = createField( 20, 18 );
			labelMapInfo.text = 'map info';

			labelBaseTextureSize = createField( 50, 8 );
			labelRectTileDimension = createField( 70, 8 );
			labelMapTilesCount = createField( 90, 8 );

			// ...
			labelViewInfo = createField( 120, 18 );
			labelViewInfo.text = 'view info';
			labelTilePos = createField( 150, 8, 50 );

			labelRectPos = createField( 190, 8, 50 );

			labelIsoPos = createField( 230, 8, 50 );

			UI.registerUIHandlers( null, onMouseMove );
		}

		private function createField( py:Number, size:uint, height:uint = 0 ):TextField
		{
			var t:TextField = new TextField( 130, height == 0 ? 20 : height, "", BitmapFont.MINI, size, 0xFFFFFF );
			t.hAlign = HAlign.LEFT;
			t.vAlign = VAlign.TOP;
			t.x = 10;
			t.y = py;
			addChild( t );
			return t;
		}

		public function setBaseTextureSize( iW:uint, iH:uint ):void
		{
			labelBaseTextureSize.text = 'base texture size : ' + iW + 'x' + iH;
		}

		public function setRectTileDimension( rW:uint, rH:uint ):void
		{
			labelRectTileDimension.text = 'rect tile dimension : ' + rW + 'x' + rH;
		}

		public function setMapTilesCount( sW:uint, sH:uint ):void
		{
			labelMapTilesCount.text = 'map tiles : ' + sW + 'x' + sH + ' (' + (sW * sH) + ')';
		}

		public function setTilePos( tX:uint, tY:uint ):void
		{
			labelTilePos.text = 'tile pos :\n---------\nTX : ' + tX + '\nTY : ' + tY;
		}

		public function setRectPos( rX:uint, rY:uint ):void
		{
			labelRectPos.text = 'rect pos :\n---------\nRX : ' + rX + '\nRY : ' + rY;
		}

		public function setIsoPos( iX:int, iY:int ):void
		{
			labelIsoPos.text = 'tile iso :\n---------\nIX : ' + iX + '\nIY : ' + iY;
		}

		public function setBGHeight( h:Number ):void
		{
			bg.height = h;
		}

		public static function getInstance( mc:MovieClip = null ):InfoPanel
		{
			if (_instance == null) _instance = new InfoPanel( mc );
			return _instance;
		}

		private function onMouseMove():void
		{
			var pi:Point = UI.cursor.getIsoPosition();
			var p:Point = UI.cursor.getRectPosition();
			var tp:Point = new Point( Math.floor( p.x / MapGrid.TILE_W ), Math.floor( p.y / MapGrid.TILE_H ) );
			setTilePos( tp.x, tp.y );
			setRectPos( p.x, p.y );
			setIsoPos( pi.x, pi.y );
		}
	}
}